﻿// Copyright (C) 2009 - 2013 EvTech Project
//
// This program is free software: you can redistribute it and/or modify
// it under the +terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Drawing;

namespace Piirto
{
    class ImagePrintDocument : PrintDocument
    {
        Image image;

        public ImagePrintDocument(Image image)
        {
            this.image = image;
            if (image != null && image.Height > image.Width)
            {
                this.DefaultPageSettings.Landscape = false;
            }
            else
            {
                this.DefaultPageSettings.Landscape = true;
            }

            this.PrintPage += new PrintPageEventHandler(ImagePrintDocument_PrintPage);
        }

        void ImagePrintDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            Rectangle rec = e.MarginBounds;
            String printTime = "Tulostettu " + DateTime.Now.ToString("d.M.yyyy");
            if (image != null)
            {
                if (rec.Width / rec.Height > image.Width / image.Height)
                {
                    rec.Width = image.Width * rec.Height / image.Height;
                    rec.X = rec.X + (e.MarginBounds.Width - rec.Width) / 2;
                }
                else
                {
                    rec.Height = image.Height * rec.Width / image.Width;
                    rec.Y = rec.Y + (e.MarginBounds.Height - rec.Height) / 2;
                }
                e.Graphics.DrawImage(image, rec);
                SizeF textSize = e.Graphics.MeasureString(printTime, SystemFonts.DefaultFont);
                e.Graphics.FillRectangle(Brushes.White, 0, 0, textSize.Width, textSize.Height);
                e.Graphics.DrawString(printTime, SystemFonts.DefaultFont, Brushes.Black, new PointF(0, 0));
            }
            e.HasMorePages = false;
        }
    }
}
